package acm.security.session.listener;
/**
 * @author Emporeo
 * @web http://www.fai-communication.de/accountManager
 * @web source: http://code.google.com/p/mmo-account-manager/
 */
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public final class SessionListener implements HttpSessionListener {

	public int sessionCount;
	
	public SessionListener() {
		this.sessionCount = 0;
    }

    public void sessionCreated(HttpSessionEvent sessionEvent) {

        // Get the session that was created
        HttpSession session = sessionEvent.getSession();
        
        synchronized (this) {
            sessionCount++;
          }

        // Store something in the session, and log a message
        try {
            session.getServletContext().log("[ACM - SessionListener] Session created: "+session);
            session.getServletContext().log("[ACM - SessionListener] Session count: "+sessionCount);
        } catch (Exception e) {
        	session.getServletContext().log("[ACM- SessionListener] Error setting session attribute: " + e.getMessage());
        }
    }

    public void sessionDestroyed(HttpSessionEvent sessionEvent) {

        // Get the session that was invalidated
        HttpSession session = sessionEvent.getSession();
        
        synchronized (this) {
            --sessionCount;
          }

        // Log a message
        session.getServletContext().log("[ACM -SessionListener] Session invalidated: "+session);
        session.getServletContext().log("[ACM - SessionListener] Session count: "+sessionCount);
    }
    
}
